/*
 * jGrouse Console
 * Copyright (c) 2007 by Denis Riabtchik
 * See http://jgrouse.com/license.txt for licensing details
 */

jgrouse_console = 
{
	_initLocation : function()
	{
		if (!jgrouse_console.location)
		{
			var scripts = document.getElementsByTagName('script');
			for (var i = 0; i < scripts.length; i++)
			{
				var s = scripts[i].src;
				if (s)
				{
					var arr = s.split('/');
					if (arr[arr.length - 1] == 'jgconsole.js')
					{
						arr.splice(arr.length - 1, 1);
						jgrouse_console.location = arr.join('/');
						return;
					}
				}
			}	
		}
	},
	
	isAutoInit : function()
	{
		var scripts = document.getElementsByTagName('script');
		for (var i = 0; i < scripts.length; i++)
		{
			var s = scripts[i].src;
			if (s)
			{
				var arr = s.split('/');
				if (arr[arr.length - 1] == 'jgconsole.js')
				{
					var ai = scripts[i].attributes['autoinit'];
					if (!ai)
					{
						return null;
					}
					return ai.nodeValue.toLowerCase();
				}
			}
		}	
		return null;
	},
	
	autoInit : function()
	{
		var ai =  jgrouse_console.isAutoInit();
		if (ai == 'link')
		{
			jgrouse_console.createConsoleSpot();
		}
		else if (ai == 'popup')
		{
			jgrouse_console.initialize();
		}
		jg_console.removeListener(window, 'load', arguments.callee)
	},
	
	
	_logItems : [],
	
	initialize : function()
	{
		jgrouse_console._initLocation();
		var loc = jgrouse_console.location + '/console.html'
		jgrouse_console.hwnd = window.open(loc, 
			'jgrouse_console_window', 'resizable :1, status=yes,toolbar=no,menubar=no,location=no');
		jg_console.addListener(window, 'unload', jgrouse_console._unload);
	},
	
	initLogContent : function()
	{
		for (var i = 0; i < jgrouse_console._logItems.length; i++)
		{
			jg_console.addLog(jgrouse_console._logItems[i]);
		}
	},
	
	_unload : function()
	{
		if (jgrouse_console.hwnd)
		{
			jgrouse_console.hwnd.close();
		}
	},
	
	
	run : function(msg)
	{
		try
		{
			return eval(msg);
		}
		catch (ex)
		{
			return "Exception: " + ex.toString();
		}
	},
	
	createConsoleSpot : function()
	{
		jgrouse_console._initLocation();
		var a = document.createElement('a');
		a.href = "javascript:jgrouse_console.initialize()";
		var i = document.createElement("img");
		i.src = jgrouse_console.location + '/favicon.ico';
		a.appendChild(i);
		a.style.cssText="position:absolute; top:0px; right:0px";
		document.getElementsByTagName('body')[0].appendChild(a);
	}
}


jg_console = 
{
	runEval : function()
	{
		var d = document.getElementById("expr");
		var w = window.opener;
		var jg = w.jgrouse_console;
		var result = jg.run(d.value);
		var res = document.getElementById('results');
		var tmp = document.createElement('div');
		tmp.appendChild(document.createTextNode(result));
		if (res.firstChild)
		{
			res.replaceChild(tmp, res.firstChild);
		}
		else
		{
			res.appendChild(tmp);
		}
		jg_console.addItem(d.value, result);
	},
	
	empty : function()
	{
	},
	
	histClicked : function(event)
	{
		if (!event)
		{
			event = window.event;
		}
		var src = event.target || event.srcElement;
		var d = document.getElementById("expr");
		d.value = src.firstChild.nodeValue;
	},
	
	
	addItem : function(s, result)
	{
		var container = document.getElementById('history');
		var d = document.createElement("div");
		d.className = 'histItem';
		
		var a = document.createElement('a');
		a.appendChild(document.createTextNode(s));
		a.href="javascript:jg_console.empty()";
		a.className	= "hist";
		a.title = result;
		jg_console.addListener(a, 'click', jg_console.histClicked);
		d.appendChild(a);
		container.insertBefore(d, container.firstChild);
	},	
	
	addLog : function(s)
	{
		var doc = jgrouse_console.hwnd.document;
		var container = doc.getElementById('log');
		
		var d = doc.createElement("div");
		d.className = 'histItem';
		d.style.paddingLeft = '3px';
		d.style.paddingRight = '3px';
		d.appendChild(doc.createTextNode(s));
		d.title = s;
		container.appendChild(d);
	},
	
	log : function(s)
	{
		jgrouse_console._logItems.push(s);
		if (jgrouse_console.hwnd)
		{
			jg_console.addLog(s);
		}
	},
	
	clearItems : function()
	{
		var container = document.getElementById('history');
		var node = container.lastChild;
		while (node)
		{
			var a = node.firstChild;
			if (a)
			{
				jg_console.removeListener(a, 'click', jg_console.histClicked);
			}
			node = node.previousSibling;
		}
		container.innerHTML = '';
	},
	
	unload : function()
	{
		jg_console.removeListener(window, 'unload', jg_console.unload);
		jg_console.clearItems();	
	},
	
	addListener : function(element, eventName, handler)
	{
		if (element.addEventListener)
		{
			element.addEventListener(eventName, handler, false);
		}
		else
		{
			element.attachEvent('on' + eventName, handler);
		}
	},
	
	removeListener : function(element, eventName, handler)
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(eventName, handler, false);
		}
		else
		{
			element.detachEvent('on' + eventName, handler);
		}
	}
	
}

jg_console.addListener(window, 'load', jgrouse_console.autoInit);		

if (typeof console == 'undefined')
{
	console = jg_console;
}